package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.common.exception.CustException;
import com.heima.model.admin.dtos.SensitiveDTO;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {

    /**
     * 敏感词查询
     * @param sensitiveDTO
     * @return
     */
    @Override
    public ResponseResult list(SensitiveDTO sensitiveDTO) {
        //判空
        if(sensitiveDTO == null ){
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID);
        }
        sensitiveDTO.checkParam();
        //构建查询条件
        LambdaQueryWrapper<AdSensitive> wrapper = Wrappers.lambdaQuery();
        //根据敏感词查询
        if(StringUtils.isNotBlank(sensitiveDTO.getName())){
            wrapper.like(AdSensitive::getSensitives,sensitiveDTO.getName());
        }
        //分页
        Page<AdSensitive> page = new Page<>();
        //查询
        IPage<AdSensitive> sensitiveIPage = this.page(page,wrapper);

        ResponseResult responseResult = new PageResponseResult(sensitiveDTO.getPage(),sensitiveDTO.getSize(), sensitiveIPage.getTotal(),sensitiveIPage.getRecords());

        return responseResult;
    }

    /**
     * 敏感词新增
     * @param sensitive
     * @return
     */
    @Override
    public ResponseResult insert(AdSensitive sensitive) {
        if(StringUtils.isBlank(sensitive.getSensitives())){
           CustException.cust(AppHttpCodeEnum.PARAM_INVALID,"敏感词不能为空");
        }
        int count = this.count(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives, sensitive.getSensitives()));
        if(count > 0){
            CustException.cust(AppHttpCodeEnum.DATA_EXIST,"敏感词已存在,请勿重复");
        }
        sensitive.setCreatedTime(new Date());
        this.save(sensitive);

        return ResponseResult.okResult();
    }

    //敏感词修改
    @Override
    public ResponseResult update(AdSensitive sensitive) {
        AdSensitive adSensitive = getById(sensitive);
        if(adSensitive == null){
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        int count = count(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives, sensitive.getSensitives()));
        if(count > 0){
            CustException.cust(AppHttpCodeEnum.DATA_EXIST,"已存在该敏感词");
        }
        updateById(sensitive);

        return ResponseResult.okResult();
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteById(Integer id) {
        AdSensitive sensitive = getById(id);
        if(sensitive == null){
            CustException.cust(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST);
        }
        removeById(id);
        return ResponseResult.okResult();
    }

    @Autowired
    AdSensitiveMapper adSensitiveMapper;
    /**
     * 查询铭感词列表
     * @return
     */
    @Override
    public ResponseResult<List<String>> selectAllSensitives() {
        return ResponseResult.okResult(adSensitiveMapper.findAllSensitives());
    }
}
